import { Form } from 'antd';
import { history } from 'umi';
import { getKeyRouter } from '@/utils';
import { useEffect, useMemo, useState } from 'react';
import { setTableRowClassName } from './Format';
import { ApplyManageTabSwitch } from '../mconfig';
import { ApplyManageService } from '../services/ManageService';
import { DictionaryService } from '../services/DictionaryService';
import { getApplyManageTableColumns, getApplyManageSearchColumns, getApplyManageTableHeaderActions } from '../mconfig/index';
import { ApplyManageAPI } from './type';

/**
 * @useNeedManage 管理页hooks
 * @param curRes 权限信息
 * @returns
 */
export function useApplyManage({ curRes, locationProps }: ApplyManageAPI.IManageType) {
  /** 作废申请表单实例 */
  const [cancelForm] = Form.useForm();

  /** 作废申请渲染状态 */
  const [isShowCancel, setIsShowCancel] = useState<Record<string, any>>({});

  /** 更改作废状态方法 */
  const onChangeCancelModalState = (state: Record<string, any>) => setIsShowCancel(state);

  /**撤销弹窗*/
  const [invalidState, setInvalidState] = useState<Record<string, any>>({});

  /** 字典 */
  const { supplyWayList, applyStateList, cancelTypeList, checkStateList } = DictionaryService();

  /** dataGrid 接口 */
  const { applyManageRun, applyManageDataSource, applyManageLoading, applyManageTableProps } = ApplyManageService(curRes).getApplyManageService();

  const [isOperation, setIsOperation] = useState(false);
  /** 作废事件 */
  const submitCancel = () => {
    return new Promise(async (resolve) => {
      const result = await cancelForm.validateFields();
      resolve(true);
    });
  };

  /** 管理页操作事件 */
  const operation = {
    onAdd: () => {
      history.push({
        pathname: `/purchaseApplyController/addApply/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          type: 'add',
          supplyWay: 1,
          source: 'apply',
          returnPathName: locationProps?.pathname,
        },
      });
    },
    onEdit: (record: Record<string, any>) => {
      history.push({
        pathname: `/purchaseApplyController/editApply/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          type: 'edit',
          info: record,
          source: 'apply',
          returnPathName: locationProps?.pathname,
        },
      });
    },
    onView: (record: Record<string, any>) => {
      // // 跳转详情页面
      history.push({
        pathname: `/purchaseApplyController/getApplyDetails/{id}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          type: 'view',
          info: record,
          source: 'apply',
          step: 0,
          returnPathName: locationProps?.pathname,
        },
      });

      // 跳转OA页面
      // history.push({
      //   pathname: `/purchaseApplyController/dealHomePage/:${getKeyRouter()}${getKeyRouter()}`,
      //   state: {
      //     pageType: 'detail',
      //     id: record?.id,
      //     pageInfo: { id: record?.id },
      //     taskId: record?.id,
      //     pIDDetailInfo: { id: record?.id },
      //   },
      // });
    },
    onCancel: (record: Record<string, any>) => {
      setIsOperation(true);
      onChangeCancelModalState({ resObj: record, isShow: true });
    },
    onInvalid: (record) => {
      setInvalidState({
        isShow: true,
        resObj: record,
      });
    },
  };

  /** 头部权限切换 */
  const switchList = useMemo(() => {
    return [];
    return ApplyManageTabSwitch(curRes);
  }, [curRes]);

  /** 表格配置 */
  const tableProps = {
    columns: getApplyManageTableColumns({ operation }, curRes),
    dataSource: applyManageDataSource,
    // rowClassName: setTableRowClassName,
    loading: applyManageLoading,
    pagination: applyManageTableProps?.pagination,
    rowKey: 'id',
    onPageChange: (current: number, pageSize: number) => {
      applyManageRun({ current, pageSize });
    },
  };

  /** 表格顶部查询条件配置 */
  const searchProps = {
    switchList: switchList,
    onSwitch: (_: string | undefined | number, searchValues: any, params: any) => {
      // applyManageRun({ ...searchValues, ...params });
    },
    searchList: getApplyManageSearchColumns({ supplyWayList, applyStateList, checkStateList, cancelTypeList }),
    onSearch: (params: any) => {
      applyManageRun({ ...params });
    },
    onReset: (params: any) => {
      applyManageRun({ ...params });
    },
  };

  /** 表格顶部按钮配置 */
  const actionList = getApplyManageTableHeaderActions(operation, curRes);

  /** 更新管理页方法 */
  const onReloadManage = () => {
    /** 解构上一次请求的分页数据 */
    let { current = 1, pageSize = 10 } = applyManageTableProps?.pagination;
    /** 获取管理页数据方法 */
    applyManageRun({ current, pageSize });
  };

  return {
    operation,
    tableProps,
    searchProps,
    actionList,
    cancelForm,
    isShowCancel,
    isOperation,
    onChangeCancelModalState,
    submitCancel,
    onReloadManage,
    invalidState,
    setInvalidState,
  };
}
